/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xml;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.jdom.JDOMException;

/**
 *
 * @author Administrador
 */
public class Consumo {

    private List viajes;
    private Consumo demo;
    private String rutaViaje;

    public Consumo() {
    }

    public String obtenerXML(String username) {
        System.out.println("hola");
        String Xml = "";
       // String URL = "http://192.168.21.83:8000/travelernet/rest/"+username;
         //String URL = "http://192.168.22.137:8080/hayTourViajes/rest/usuario/"+username;
        String URL = "http://192.168.22.161:8080/ServicioDesarrollo/viajes/listarViajes3/"+username;
        try {
            InputStream is = get(URL);
            Xml = convertStreamToString(is);
        } catch (Exception ex) {
            java.util.logging.Logger.getLogger(Consumo.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println(Xml.toString());
        return Xml.toString();
    }

    public String obtenerXMLItin(String id,String username) {
        System.out.println("hola");
        String Xml2 = "";
       // String URL = "http://192.168.21.83:8000/travelernet/rest/"+username+"/"+id;
         //String URL = "http://192.168.22.137:8080/hayTourViajes/rest/viaje/"+id;
         String URL = "http://192.168.22.161:8080/ServicioDesarrollo/viajes/itinerario/"+id;
        try {
            InputStream is = get(URL);
            //InputStream is = get("http://192.168.1.101:8080/hayTourViajes/rest/usuario/gzerpa");
            Xml2 = convertStreamToString(is);
        } catch (Exception ex) {
            java.util.logging.Logger.getLogger(Consumo.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("CONSUMO ITINE: " + Xml2.toString());
        return Xml2.toString();
    }

    public String convertStreamToString(InputStream is) throws IOException {
        /*
         * To convert the InputStream to String we use the
         * Reader.read(char[] buffer) method. We iterate until the
         * Reader return -1 which means there's no more data to
         * read. We use the StringWriter class to produce the string.
         */
        if (is != null) {
            Writer writer = new StringWriter();

            char[] buffer = new char[1024];
            try {
                Reader reader = new BufferedReader(
                        new InputStreamReader(is, "UTF-8"));
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                is.close();
            }
            return writer.toString();
        } else {
            return "";
        }
    }

    public static InputStream get(String urlStr) throws Exception {
        return readFrom(urlStr, null);
    }

    public static InputStream post(String urlStr, String post) throws Exception {
        return readFrom(urlStr, post);
    }

    private static InputStream readFrom(String urlStr, String postStr) throws MalformedURLException, IOException {
        URLConnection conn = new URL(urlStr).openConnection();
        conn.setDoInput(true);
        if (postStr != null && postStr.length() > 0) {
            conn.setDoOutput(true);
            DataOutputStream output =
                    new DataOutputStream(conn.getOutputStream());
            output.writeBytes(postStr);
            output.flush();
            output.close();
        }
        return conn.getInputStream();
    }
}
